package com.texaco.chapasFutbol.scene;

import java.util.ArrayList;
import java.util.List;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.background.SpriteBackground;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.util.ActivityUtils;
import org.andengine.util.call.Callback;
import org.andengine.util.progress.IProgressListener;
import org.andengine.util.progress.ProgressCallable;

import com.texaco.chapasFutbol.ChapasFutbolActivity;
import com.texaco.chapasFutbol.R;
import com.texaco.chapasFutbol.buttons.Button;
import com.texaco.chapasFutbol.buttons.IButtonCallBack;
import com.texaco.chapasFutbol.entidades.Game;
import com.texaco.chapasFutbol.resources.Resources;

/**
 * Clase que implementa la escena de la pantalla principal.
 * @author Manuel
 */
public class MenuPrincipalScene extends SceneBase{

	/*
	 * CAPAS
	 */
	public final static int LAYER_OPTIONS = 0;
	public final static int LAYER_TUTORIAL = LAYER_OPTIONS + 1;
	
	private boolean mToggleSound = true;
	private boolean mToggleMusic = true;
	
	/*
	 * Botones para el sonido
	 */
	Sprite bMusicOn;
	Sprite bMusicOff;
	Sprite bSfxOn;
	Sprite bSfxOff; 
	
	TutorialScene tutorial;

	public MenuPrincipalScene(Game game){
		super(game);
	}

	@Override
	public void initAsync(IProgressListener pProgressListener) {
		tutorial = game.getSceneHandler().getTutorial();

		android.content.res.Resources res = this.game.getActivity().getResources();
		final ChapasFutbolActivity activity = this.game.getActivity();
		ITextureRegion bSigSh = activity.getShapesMaps().get(Game.BOTON_SIGUIENTE_SHAPE);
		Camera cam = activity.getEngine().getCamera();
		
		float margenLeft = res.getInteger(R.integer.layout_left_margin);
		float margenRight = res.getInteger(R.integer.layout_right_margin);
		float margenBottom = res.getInteger(R.integer.layout_bottom_margin);
		float b2jugadoresX = margenLeft;
		float b2jugadoresY = margenLeft;
		float bMultiOnlineX = (margenLeft*2) + 189;
		float bMultiOnlineY = margenLeft;
		float bOpcionesX = margenLeft;
		float bOpcionesY = (margenLeft*2) + 205;
		float bTutorialX = cam.getWidth() - bSigSh.getWidth() - margenRight;
		float bTutorialY = cam.getHeight() - bSigSh.getHeight() - margenBottom;

		Sprite principalBg = new Sprite(0, 0, 
				this.game.getActivity().getShapesMaps().get(
				Game.PRINCIPAL_BACKGROUND_SHAPE), 
				this.game.getActivity().getVertexBufferObjectManager());
		this.setBackground(new SpriteBackground(principalBg));

		List<String> textos = new ArrayList<String>();
		textos.add((String) res.getText(R.string.mpp_2));
		textos.add((String) res.getText(R.string.mpp_jugadores));
		
		List<Font> fuentes = new ArrayList<Font>();
		fuentes.add(this.game.getActivity().getFontMap().get(Resources.FONT_PRINCIPAL_BIG));
		fuentes.add(this.game.getActivity().getFontMap().get(Resources.FONT_PRINCIPAL_SMALL));
		Button b2Players = new Button(this.game, this, 
				this.getChildByIndex(LAYER_OPTIONS), 
				b2jugadoresX, b2jugadoresY, textos, fuentes, 
				Resources.PRINCIPAL_2JUGADORES_SHAPE, new IButtonCallBack() {
			public String fire() {
				game.getActivity().getSoundHandler().playFx(Resources.SOUND_CLICK);
				game.getOpciones().setOffline();
				game.loadGameScene();
				return null;
			}
		});
		
		textos = new ArrayList<String>();
		textos.add((String) res.getText(R.string.mpp_multi));
		textos.add((String) res.getText(R.string.mpp_online));
		
		Button bMultiOnline = new Button(this.game, this, 
				this.getChildByIndex(LAYER_OPTIONS), 
				bMultiOnlineX, bMultiOnlineY, textos, fuentes, 
				Resources.PRINCIPAL_MULTI_ONLINE_SHAPE, new IButtonCallBack() {
			public String fire() {
				game.getActivity().getSoundHandler().playFx(Resources.SOUND_CLICK);
				game.getOpciones().setOnline();
				game.loadMultiplayerMatchScene();
				return null;
			}
		});
		
		textos = new ArrayList<String>();
		textos.add((String) res.getText(R.string.mpp_opciones));
		
		fuentes = new ArrayList<Font>();
		fuentes.add(this.game.getActivity().getFontMap().get(Resources.FONT_PRINCIPAL_SMALL));
		
		Button bOpciones= new Button(this.game, this, 
				this.getChildByIndex(LAYER_OPTIONS), 
				bOpcionesX, bOpcionesY, textos, fuentes, 
				Resources.PRINCIPAL_OPCIONES_SHAPE, new IButtonCallBack() {
			public String fire() {
				game.getActivity().getSoundHandler().playFx(Resources.SOUND_CLICK);
				game.loadOptionScene();
				return null;
			}
		});
		
		Button bTutorial = new Button(this.game, this, 
				this.getChildByIndex(LAYER_OPTIONS), 
				bTutorialX, bTutorialY, 
				R.string.tLabelButtonTutorial, 
				this.game.getActivity().getFontMap().get(Resources.FONT_OPTIONS), 
				Game.BOTON_SIGUIENTE_SHAPE, //FIXME: PASAR A RESOURCES. 
				new IButtonCallBack() {
			public String fire() {
				game.getActivity().getSoundHandler().playFx(Resources.SOUND_CLICK);
				
				if(!tutorial.isInitiated()){
					game.getActivity().runOnUiThread(new Runnable() {
						public void run() {
							ActivityUtils.doProgressAsync(game.getActivity(), 
									R.string.texto_cargando_escena, android.R.drawable.ic_menu_rotate, 
									new ProgressCallable<Void>() {
								public Void call(final IProgressListener pProgressListener) 
										throws Exception {
									tutorial.init(pProgressListener);
									
									pProgressListener.onProgressChanged(100);
									tutorial.showTutorial();
									MenuPrincipalScene.this.setChildSceneModal(tutorial);
									return null;
								}
							}, new Callback<Void>() {
								public void onCallback(final Void pCallbackValue) {
									/* Nothing. */
								}
							});
						}
					});
				}else{
					tutorial.update();
					tutorial.showTutorial();
					MenuPrincipalScene.this.setChildSceneModal(tutorial);
				}
				return null;
			}

		});
		
		/*
		 * Botón para la música.
		 */
		ITextureRegion musicOnTexture = activity.getVectorMap().get(Resources.VECTOR_MUSIC_ON);
		bMusicOn = new Sprite( (activity.getEngine().getCamera().getWidth()/2) - 
				musicOnTexture.getWidth() - 50, 
				activity.getEngine().getCamera().getHeight() - musicOnTexture.getHeight() - 50,
				musicOnTexture,
				activity.getVertexBufferObjectManager()) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionUp()) {
					activity.getSoundHandler().playFx(Resources.SOUND_CLICK);
					activity.getSoundHandler().toogleMusic();
					toggleMusicResource();
				}
				return true;
			}
		};
		this.attachChild(LAYER_OPTIONS, bMusicOn);
		this.registerTouchArea(bMusicOn);
		
		ITextureRegion musicOffTexture = activity.getVectorMap().get(Resources.VECTOR_MUSIC_OFF);
		bMusicOff = new Sprite( (activity.getEngine().getCamera().getWidth()/2) - 
				musicOffTexture.getWidth() - 50, 
				activity.getEngine().getCamera().getHeight() - musicOffTexture.getHeight() - 50,
				musicOffTexture,
				activity.getVertexBufferObjectManager()) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionUp()) {
					activity.getSoundHandler().playFx(Resources.SOUND_CLICK);
					activity.getSoundHandler().toogleMusic();
					toggleMusicResource();
				}
				return true;
			}
		};
		
		/*
		 * Botón para el sonido.
		 */
		ITextureRegion sfxOnTexture = activity.getVectorMap().get(Resources.VECTOR_SFX_ON);
		bSfxOn = new Sprite(
				(activity.getEngine().getCamera().getWidth()/2) + 50, 
				activity.getEngine().getCamera().getHeight()- sfxOnTexture.getHeight() - 50, 
				sfxOnTexture,
				activity.getVertexBufferObjectManager()) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionUp()) {
					activity.getSoundHandler().playFx(Resources.SOUND_CLICK);
					activity.getSoundHandler().toogleFx();
					toggleSoundResource();
				}
				return true;
			}
		};
		this.attachChild(LAYER_OPTIONS, bSfxOn);
		this.registerTouchArea(bSfxOn);

		ITextureRegion sfxOffTexture = activity.getVectorMap().get(Resources.VECTOR_SFX_OFF);
		bSfxOff = new Sprite(
				(activity.getEngine().getCamera().getWidth()/2) + 50, 
				activity.getEngine().getCamera().getHeight() - sfxOffTexture.getHeight() - 50, 
				sfxOffTexture,
				activity.getVertexBufferObjectManager()) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionUp()) {
					activity.getSoundHandler().playFx(Resources.SOUND_CLICK);
					activity.getSoundHandler().toogleFx();
					toggleSoundResource();
				}
				return true;
			}
		};
	}

	/**
	 * Se cambia la imagen del sonido. On->Off, Off->On
	 */
	private void toggleSoundResource() {
		ChapasFutbolActivity activity = game.getActivity();
		this.mToggleSound = !this.mToggleSound;
		if(!this.mToggleSound){
			/*
			 * Quitar el boton de sfx on de la escena y poner el de sfx off.
			 */
			activity.getEngine().getEngineLock().lock();
			this.unregisterTouchArea(bSfxOn);
			this.detachChild(LAYER_OPTIONS, bSfxOn);
			activity.getEngine().getEngineLock().unlock();
			
			this.attachChild(LAYER_OPTIONS, bSfxOff);
			this.registerTouchArea(bSfxOff);
		}else{
			/*
			 * Quitar el boton de sfx off de la escena y poner el de sfx on.
			 */
			activity.getEngine().getEngineLock().lock();
			this.unregisterTouchArea(bSfxOff);
			this.detachChild(LAYER_OPTIONS, bSfxOff);
			activity.getEngine().getEngineLock().unlock();
			
			this.attachChild(LAYER_OPTIONS, bSfxOn);
			this.registerTouchArea(bSfxOn);
		}
	}
	
	/**
	 * Se cambia la imagen de la música. On->Off, Off->On
	 */
	private void toggleMusicResource() {
		ChapasFutbolActivity activity = game.getActivity();
		this.mToggleMusic = !this.mToggleMusic;
		if(!this.mToggleMusic){
			/*
			 * Quitar el boton de music on de la escena y poner el de music off.
			 */
			activity.getEngine().getEngineLock().lock();
			this.unregisterTouchArea(bMusicOn);
			this.detachChild(LAYER_OPTIONS, bMusicOn);
			activity.getEngine().getEngineLock().unlock();
			
			this.attachChild(LAYER_OPTIONS, bMusicOff);
			this.registerTouchArea(bMusicOff);
		}else{
			/*
			 * Quitar el boton de music off de la escena y poner el de music on.
			 */
			activity.getEngine().getEngineLock().lock();
			this.unregisterTouchArea(bMusicOff);
			this.detachChild(LAYER_OPTIONS, bMusicOff);
			activity.getEngine().getEngineLock().unlock();
			
			this.attachChild(LAYER_OPTIONS, bMusicOn);
			this.registerTouchArea(bMusicOn);
		}

		
		if(this.game.getActivity().getSoundHandler().isMusicPlaying()){
			this.game.getActivity().getSoundHandler().pauseMusic();
		}else{
			this.game.getActivity().getSoundHandler().playMusic(Resources.MUSIC_PRINCIPAL, true);
		}
	}
	
	@Override
	protected void update() {
		// TODO Auto-generated method stub
	}
	
	@Override
	protected int layerCount(){
		return 2;
	}

	@Override
	protected boolean async() {
		return true;
	}
}
